package com.shinbada.modules.competitor.web;

import com.shinbada.excel.modle.ImportExcelResult;
import com.shinbada.excel.modle.UploadModle;
import com.shinbada.modules.competitor.entity.VO.CompetitorProductDataItemVO;
import com.shinbada.modules.competitor.utils.TreeUtilCommon;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.shinbada.common.json.AjaxJson;
import com.shinbada.core.persistence.Page;
import com.shinbada.core.web.BaseController;
import com.shinbada.common.utils.StringUtils;
import com.shinbada.modules.competitor.entity.CompetitorProductDataItem;
import com.shinbada.modules.competitor.service.CompetitorProductDataItemService;

import com.shinbada.excel.ExcelKit;

/**
 * 产品数据项Controller
 * @author initcap
 * @version 2021-08-04
 */
@RestController
@RequestMapping(value = "/competitor/competitorProductDataItem")
@Api(tags = "产品数据项")
public class CompetitorProductDataItemController extends BaseController {

	@Autowired
	private CompetitorProductDataItemService competitorProductDataItemService;

	@ModelAttribute
	public CompetitorProductDataItem get(@RequestParam(required=false) String id) {
		CompetitorProductDataItem entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = competitorProductDataItemService.get(id);
		}
		if (entity == null){
			entity = new CompetitorProductDataItem();
		}
		return entity;
	}

	@RequiresPermissions("user")
	@GetMapping("list")
	@ApiOperation("产品数据项列表数据")
	public AjaxJson list(CompetitorProductDataItem competitorProductDataItem, HttpServletRequest request, HttpServletResponse response) {
		Page<CompetitorProductDataItem> page = competitorProductDataItemService.findPage(new Page<>(request, response), competitorProductDataItem);
		return AjaxJson.success().put("page",page);
	}

	@RequiresPermissions("user")
	@GetMapping("queryById")
	@ApiOperation("根据Id获取产品数据项数据")
	public AjaxJson queryById(CompetitorProductDataItem competitorProductDataItem) {
		return AjaxJson.success().put("competitorProductDataItem", competitorProductDataItem);
	}

	@RequiresPermissions(value={"competitor:competitorProductDataItem:add","competitor:competitorProductDataItem:edit"},logical=Logical.OR)
	@PostMapping("save")
	@ApiOperation("保存产品数据项")
	public AjaxJson save(CompetitorProductDataItem competitorProductDataItem) {
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(competitorProductDataItem);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		competitorProductDataItemService.save(competitorProductDataItem);
		return AjaxJson.success("保存产品数据项成功");
	}

	@RequiresPermissions("competitor:competitorProductDataItem:del")
	@DeleteMapping("delete")
	@ApiOperation("批量删除产品数据项")
	public AjaxJson delete(String ids) {
		String[] idArray =ids.split(",");
		for(String id : idArray){
			competitorProductDataItemService.delete(competitorProductDataItemService.get(id));
		}
		return AjaxJson.success("删除产品数据项成功");
	}

	@RequiresPermissions("competitor:competitorProductDataItem:export")
	@GetMapping("export")
	@ApiOperation("导出产品数据项")
	public void export(CompetitorProductDataItem entity, HttpServletResponse response) {
		ExcelKit.$Export(CompetitorProductDataItem.class, response).downXlsx(competitorProductDataItemService.findList(entity), false);
	}

	@RequiresPermissions("competitor:competitorProductDataItem:import")
	@GetMapping("template")
	@ApiOperation("导出产品数据项模版")
	public void export(HttpServletResponse response) {
		ExcelKit.$Export(CompetitorProductDataItem.class, response).downXlsx(new ArrayList<>(), true);
	}

	@RequiresPermissions("competitor:competitorProductDataItem:import")
	@PostMapping(value = "import")
	@ApiImplicitParam(name = "modle", paramType = "query", allowableValues = "skip,exit,cover")
	@ApiOperation("导入文件,下载模版为/export")
	public AjaxJson uploadExcel(MultipartFile file, UploadModle modle) {
		if (shinProperites.isDemoMode()) {
			return AjaxJson.error("演示模式，不允许操作！");
		}
		ImportExcelResult<CompetitorProductDataItem> result = competitorProductDataItemService.uploadExcel(file, modle);
		return AjaxJson.success(result);
	}
	@RequiresPermissions("user")
	@GetMapping("getTreeStructure")
	@ApiOperation("数据项结构树")
	public AjaxJson treeStructure(@Param("dataItemType") String dataItemType,@Param("parameterType") String parameterType) throws Exception {
		List<CompetitorProductDataItemVO> competitorProductDataItemList =competitorProductDataItemService.getTreeStructure(dataItemType,parameterType);
		List<CompetitorProductDataItemVO> competitorResultVOS = TreeUtilCommon.buildTree(competitorProductDataItemList, "com.shinbada.modules.competitor.entity.VO.CompetitorProductDataItemVO", "dataItemId", "dataItemParentId", "children");
		return AjaxJson.success().put("data",competitorResultVOS);
	}
	@RequiresPermissions("user")
	@GetMapping("addTreeStructure")
	@ApiOperation("添加属性结构树")
	public AjaxJson addTreeStructure(@Param("dataItemType")String dataItemType,@Param("parameterType") String parameterType) throws Exception {
		List<CompetitorProductDataItemVO> competitorProductDataItemList =competitorProductDataItemService.getTreeStructure(dataItemType, parameterType);
		Map<String, CompetitorProductDataItemVO> collect = competitorProductDataItemList.stream().collect(Collectors.toMap(CompetitorProductDataItemVO::getDataItemId, o -> o, (o, o1) -> o));
		return AjaxJson.success().put("data",collect);
	}
}
